<?php

namespace app\backstage\controller;

use think\Controller;
use think\Request;
use app\backstage\model\AppUserReal as realModel;

class Appuserreal extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request, realModel $model)
    {


        if($request->isAjax()){
            
            //分页
            $pageSize = 15;
            $params = $request->param();

            $page = $params['page']?:1;
            $offset = ($page-1)*$pageSize;

           $where = [];
            //搜索
            if(!empty($params['start'])){
                $where[] = ['create_time','>=',strtotime($params['start'])];
            }

            if(!empty($params['end'])){
                $where[] = ['create_time','<=',strtotime($params['end'])];
            }

            if(!empty($params['seach_text'])){
                $where[] = ['real_name|id_card','like',"%{$params['seach_text']}%"];
            }


            $data = $model->alias('a')->field('a.*,b.nickname,b.phone')->join('app_user b','a.uid=b.id','left')
                ->where($where)->order('a.id desc')->limit($offset,$pageSize)->select();


            if($data){
                foreach($data as &$v){
                    $v['create_time'] = date('Y-m-d H:i',$v['create_time']);
                }
            }

            if(!empty($where)){
                $count =  $model->where($where)->count();
            }else{
                $count = $model->count();
            }

            $pageData = ['count'=>$count,'page'=>$page,'pageSize'=>$pageSize];

            return ['code'=>200,'listData'=>$data,'pageData'=>$pageData];
           
        }
        return view('index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {   

        return view('create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if($request->isAjax()){
            
            $data = $request->post();
            $data['create_time'] = time();


            $realModel = new realModel($data);

            //插入数据
            $res = $realModel->save();
            if($res){
                return ['code'=>200,'res'=>$res];
            }else{
                return ['code'=>101,'msg'=>'插入失败'];
            }

        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $model = new realModel();
        $info = $model->alias('a')->field('a.*,b.nickname,b.phone')->join('app_user b','a.uid=b.id','left')->where('a.id',$id)->find();
        return view('edit',['details'=>$info]);
        
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if($request->isAjax()){

            $data = $request->post();
            $model = new realModel();

            //插入数据
            $res = $model->save($data,['id'=>$id]);
            if($res){
                return ['code'=>200,'res'=>$res];
            }else{
                return ['code'=>101,'msg'=>'插入失败'];
            }

        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = realModel::destroy($id);
        
        if($res){
            return ['code'=>200];
        }else{
            return ['code'=>101,'msg'=>'删除失败'];
        }
    }

     /**
     * 批量删除
     *
     * @param  string  $ids
     * @return \think\Response
     */
    public function delete_all()
    {   

        if($ids = request()->post('ids')){
            $res = realModel::destroy($ids);
            
            if($res){
                return ['code'=>200];
            }else{
                return ['code'=>101,'msg'=>'删除失败'];
            }
        }else{
            return ['code'=>101,'msg'=>'删除失败'];
        }
      
    }
}
